﻿using System;
using System.Collections.Generic;
using System.Text;
using Daco.Security;

namespace Daco.Web.Masters
{
    public class SECUREMaster : DACOMaster
    {
        public SECUREMaster()
        {
            this.Load += new EventHandler(SECUREMaster_Load);
        }

        void SECUREMaster_Load(object sender, EventArgs e)
        {
            // get the current page...
            string pageSrc = GetPageSource();

            try
            {
                Daco.Web.UI.SitePage currentPage = (Daco.Web.UI.SitePage)Page.LoadControl(pageSrc);

                if (currentPage.Permission.SelectAccess)
                {
                    CurrentPage = currentPage;
                    CurrentPage.Master = this;
                    CurrentPage.Header = Header;
                    CurrentPage.Footer = Footer;
                    CurrentPage.Navigator = Navigator;

                    // Noi dung chinh
                    Body.Right.Controls.Add(CurrentPage);

                    CPageContext.Current.CurrentPage = CurrentPage;

                    // Them bang hien thi thong tin nguoi dung hien tai day
                    Body.Left.Controls.Add(new Daco.Web.UI.WebControls.MemberInfo());

                    // add the header control before the page rendering...
                    Controls.AddAt(0, Header);
                    Controls.AddAt(1, Navigator);
                    // add the page before the page rendering...
                    Controls.Add(Body);
                    // add the footer control after the page...
                    Controls.Add(Footer);
                }
                else
                {
                    // Phat sinh permission exception, thong bao ko du tham quyen de truy cap vao tai nguyen nay
                    throw new System.UnauthorizedAccessException(Daco.Web.Resources.DException.DeniedSelect);
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                // Phat sinh application exception, thong bao tai nguyen truy xuat khong ton tai hoac khong co tham quyen
                throw new ApplicationException(Daco.Web.Resources.DACO.ResourceNotFound);
            }
        }

        protected override string GetPageSource()
        {
            string pag = string.IsNullOrEmpty(Request.QueryString["pag"]) ? "Welcome" : Request.QueryString["pag"];

            return string.Format("~/Pages/Secure/{0}.ascx", pag);
        }
    }
}
